import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';

import { FetchHightPrice, FetchPlus, FetchDiscount, FetchHotRec } from '@/network/home/index';

//高性价比
export let fetchHightPrice = createAsyncThunk('HightPrice', async (payload, { dispatch }) => {
  let res = await FetchHightPrice();

  let res1 = await FetchPlus();
  dispatch(setPlus(res1));

  let res2 = await FetchDiscount();
  dispatch(setDiscount(res2));

  let res3 = await FetchHotRec();
  dispatch(setRec(res3));

  return res;
});

export const HomeSlice = createSlice({
  name: 'home',
  initialState: {
    value: 0,
    costPerform: ['hello world'],
    plus: [], //plus房源
    disCount: {},
    hotRec: {},
  },
  reducers: {
    setPlus: (state, { payload }) => {
      state.plus = payload;
    },
    setDiscount: (state, { payload }) => {
      state.disCount = payload;
    },
    setRec: (state, { payload }) => {
      state.hotRec = payload;
    },
  },
  extraReducers(builder) {
    builder.addCase(fetchHightPrice.fulfilled, (state, action) => {
      // action is inferred correctly here if using TS

      state.costPerform = action.payload;
    });
  },
});

// Action creators are generated for each case reducer function
export const { setPlus, setDiscount, setRec } = HomeSlice.actions;

export default HomeSlice.reducer;
